/*
 * OpenBD.java
 *
 * Created on 23 de Junho de 2007, 23:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.jerm.Interface;

import org.jerm.Conectividade.RetornoSQL;
import org.jerm.Conectividade.SGBD;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author tiagofalcao
 */
public class OpenBD extends JFrame{
    private javax.swing.JButton button_ok;
    private javax.swing.JButton button_cancelar;
    private javax.swing.JButton button_novo;
    private javax.swing.JButton button_arquivo;
    private javax.swing.JComboBox combo_sgbd;
    private javax.swing.JTextField edit_database;
    private javax.swing.JTextField edit_host;
    private javax.swing.JTextField edit_porta;
    private javax.swing.JTextField edit_projeto;
    private javax.swing.JPasswordField edit_senha;
    private javax.swing.JTextField edit_usuario;
    private javax.swing.JLabel nSGBD;
    private javax.swing.JLabel nPort;
    private javax.swing.JLabel nUsuario;
    private javax.swing.JLabel nSenha;
    private javax.swing.JLabel nBaseDados;
    private javax.swing.JLabel nProjeto;
    private javax.swing.JPanel panel_login;

    private GridBagConstraints c;

    private SGBD conn;
    
    private Color COLOR_atencao;
    private Color COLOR_erro;
    private Color COLOR_sucesso;
    
    private JPanel internal;
    private boolean outros;
    
    /** Creates a new instance of OpenBD */
    public OpenBD(boolean outros) {
        super("DBModel Koyte");
        this.outros=outros;
        
        setIconImage(new ImageIcon(Principal.class.getResource("Icones/openbd.png")).getImage());//Icone
        setDefaultLookAndFeelDecorated(false); //Tema de Janela igual ao do Windows

        //Faz a Janela Ficar Identada 50 pixels para cada borda da tela
        int w = 400;
        int h = 220;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - w)/2,
                (screenSize.height - h)/2,
                  w,h);

        //Cria o Desktop de Janelas
        //desktop = new JDesktopPane(); //a specialized layered pane
        //setContentPane(desktop);
        
        //Aparetemente fecha tudo sem pedir para salvar
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        this.setResizable(false);
        //setMinimumSize(new Dimension(400,300));
        //internal=new JPanel();
        //internal.setMinimumSize(new Dimension(400,300));
        //internal.setPreferredSize (new Dimension(400,300));
        initComponentes();
        //this.add(internal);
        
        setVisible(true);
        
    }
    
        private void initComponentes(){
        AbstractAction action;
        panel_login = new javax.swing.JPanel();
        nSGBD = new javax.swing.JLabel();
        combo_sgbd = new javax.swing.JComboBox();
        nPort = new javax.swing.JLabel();
        edit_host = new javax.swing.JTextField();
        nProjeto = new javax.swing.JLabel();
        edit_projeto = new javax.swing.JTextField();
        edit_porta = new javax.swing.JTextField();
        nUsuario = new javax.swing.JLabel();
        edit_usuario = new javax.swing.JTextField();
        nSenha = new javax.swing.JLabel();
        nBaseDados = new javax.swing.JLabel();
        edit_database = new javax.swing.JTextField();
        button_ok = new javax.swing.JButton();
        button_cancelar = new javax.swing.JButton();
        button_arquivo = new javax.swing.JButton();
        button_novo = new javax.swing.JButton();
        edit_senha = new javax.swing.JPasswordField();

        panel_login.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSGBD.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSGBD.setText("SGBD disponiveis");

        combo_sgbd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mysql", "postgresql" }));
        combo_sgbd.setName("");

        nPort.setFont(new java.awt.Font("Tahoma", 3, 11));
        nPort.setText("Host / Porta:");

        edit_host.setName("");

        edit_porta.setName("");

        nUsuario.setFont(new java.awt.Font("Tahoma", 3, 11));
        nUsuario.setText("Usuario:");

        edit_usuario.setName("");

        nSenha.setFont(new java.awt.Font("Tahoma", 3, 11));
        nSenha.setText("Senha:");

        nBaseDados.setFont(new java.awt.Font("Tahoma", 3, 11));
        nBaseDados.setText("Base de Dados:");

        edit_database.setName("");
        
        nProjeto.setFont(new java.awt.Font("Tahoma", 3, 11));
        nProjeto.setText("Projeto:");

        edit_projeto.setName("");

        action = new AbstractAction("Cancelar"){
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    dispose();
                }
        };
        button_cancelar.setAction(action);

        action = new AbstractAction("Novo"){
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    new Principal().setVisible(true);;
                    dispose();
                }
        };
        button_novo.setAction(action);

        action = new AbstractAction("Arquivo"){
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                    new Principal().setVisible(true);
                    dispose();
                }
        };
        button_arquivo.setAction(action);
        
        action = new AbstractAction("OK"){
                public void actionPerformed(ActionEvent e) {
                    abrir();
                }
        };
        button_ok.setAction(action);
        //button_rest.setPreferredSize(new Dimension(60,25));
        
        
        this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        
        c.insets=new Insets(5,5,1,1);
        c.gridx = 0; //posicao x na linha
        c.gridy = 0; //posicao y na linha
        add(nSGBD,c);
        
        c.gridx=1;
        c.gridwidth=3;
        add(combo_sgbd,c);
        c.gridwidth=1;
        
        c.gridy++;///////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nPort,c);
        
        c.gridx=1;
        c.gridwidth=2;
        c.ipadx=200;
        add(edit_host,c);
        c.gridwidth=1;
        
        c.gridx=3;
        c.ipadx=60;
        add(edit_porta,c);
        c.ipadx=0;
        
        c.gridy++;///////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nBaseDados,c);
        
        c.gridx=1;
        c.gridwidth=3;
        add(edit_database,c);
        c.gridwidth=1;
        
        c.gridy++;///////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nProjeto,c);
        
        c.gridx=1;
        c.gridwidth=3;
        add(edit_projeto,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nUsuario,c);
        
        c.gridx=1;
        c.gridwidth=3;
        add(edit_usuario,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        c.gridx = 0; //posicao y na linha
        add(nSenha,c);
        
        c.gridx=1;
        c.gridwidth=3;
        add(edit_senha,c);
        c.gridwidth=1;
        
        c.gridy++;////////////////////////////////////////
        
        
        c.gridx=0;
        add(button_cancelar,c);
        
        if(outros){
            c.gridx=1;
            c.ipadx=30;
            add(button_novo,c);
            c.ipadx=0;

            c.gridx=2;
            add(button_arquivo,c);
        }
        
        c.gridx=3;
        add(button_ok,c);
        
        
        
        
        
        
        combo_sgbd.setSelectedItem("mysql");
        edit_host.setText("localhost");
        edit_porta.setText("3306");
        edit_database.setText("dbmodel_base");
        edit_usuario.setText("mc536");
        edit_projeto.setText("DBModel");
        
        
    }
        
    private void abrir(){
        try
        {
            conn = new SGBD(combo_sgbd.getSelectedItem().toString(), edit_host.getText(), edit_usuario.getText(),String.valueOf(edit_senha.getPassword()),edit_porta.getText(), edit_database.getText());
            boolean ok=conn.Conecta();
            if(ok){
                RetornoSQL r=new RetornoSQL();
                r=this.conn.executaSQL("SELECT MAX(Versao) FROM Projetos WHERE Projeto='"+edit_projeto.getText()+"';","query");
                if(r==null){
                    new Error("Nao existe este Projeto no Banco de Dados!");
                }else{
                    setVisible(false);
                    new Principal(edit_projeto.getText(),conn);
                    dispose();
                }
            }else{
                new Error("Nao foi possivel Conectar!");
            }
        }
        catch(Exception e)
        {
            setVisible(false);
            new Error("Erro grave durante tentativa de conexao");
            e.printStackTrace();
            dispose();
        }
    }
    
}
